#发射机的主程序，主板使用esp32 c3 supermini
import machine,time,_thread
from machine import Pin,ADC,SoftI2C
from esp32_i2c_1602lcd import I2cLcd
i2c = SoftI2C(sda=Pin(21),scl=Pin(22),freq=100000)
i2c_data=i2c.scan()
print(i2c_data)
i2c_data_hex = int(''.join(map(str, i2c_data))) 
lcd = I2cLcd(i2c, i2c_data_hex, 2, 16)
lcd.putstr("Car control ready")
modole_change=Pin(10,Pin.In)
led_ping=Pin(8,Pin.OUT)
led_ping.value(1)
led=Pin(9,Pin.IN)
brake=Pin(7,Pin.IN)
way_switch=Pin(6,Pin.IN)
accelerator_adc=machine.ADC(machine.Pin(4))
way_adc=machine.ADC(machine.Pin(5))
def wat():
    way_adc.read()
    print (way_adc.read())
    return way_adc.read()
def led_on():
    if led.value()==1:
        return 1
    else:
        return 0
def accelerator():
    accelerator_adc.read()
    print (accelerator_adc.read())
    return accelerator_adc.read()
#读取油门控制值

def way():
    if way_switch.value()==1:
        return 1
    else:
        return 0
#读取方向控制值

def brake():
    if brake.value()==1:
        print("brake")
        return 1
    else:return 0
#读取刹车开关

def way_switch():
    if way_switch.value()==1:
        print("way_switch")
#读取方向开关
def speed_in():
    speed_in=uart.read()
    return speed_in


uart = machine.UART(0, baudrate=9600)  # 使用UART0，设置波特率为9600
def car_online_ping():
    while True:
        uart.read()
        count=0
        time.sleep(5)
        count+=1
        if count<=5 and uart.read()=='online':
            uart.write('ping ok')
            count=0
            return "car online"
        if count>=5:
            uart.write('car offline')
            return "car offline"
        
    
while True:
    uart.write('counter ready')
    lcd.clear()
    lcd.putstr('counter ready','   wait car ready')
    data=uart.read()
    print('wait cat link')
    if data=='car ready':
        print('car ready,link OK')
        lcd.clear()
        lcd.putstr('car ready')
        break
#设备与车辆连接初始化
def modole_change_pin():
    if modole_change.value()==1:
        uart.write('manual')
        print('manual')
        lcd.clear()
        lcd.putstr('manual control')
        time.sleep(1)
        lcd.clear()
    if modole_change.value()==0:
        uart.write('auto')
        print('auto')
        lcd.clear()
        lcd.putstr('auto control')
        time.sleep(1)
        lcd.clear()
        
while True:
    _thread.start_new_thread(car_online_ping())
    _thread.start_new_thread(speed_in())
    _thread.start_new_thread(accelerator())
    _thread.start_new_thread(way())
    _thread.start_new_thread(brake())
    _thread.start_new_thread(way_switch())
    _thread.start_new_thread(wat())
    _thread.start_new_thread(led_on())
    data_out_=list(accelerator(),way(),brake(),way_switch(),way_adc)
    uart.write(data_out_)
    lcd.putstr('power:',accelerator(),'way:',way_adc(),'speed:',speed_in(),'way',way(),'breake',brake(),'led',led_on())

    if car_online_ping()=='car offline':
        while True:
            lcd.clear()
            lcd.putstr('car offline')
            uart.read()
            print('wait car link')
            if data=='car ready':
                print('car ready,link OK')
                break
            #车辆断开连接后重新连接


        
    